package com.chem.lems.handler;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.handler.CellWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Hyperlink;
import org.apache.poi.ss.usermodel.Row;

import java.util.List;

@Slf4j
public class CustomCellWriteHandler implements CellWriteHandler {
    private final String TIPS = "暂无关联的设备";
    private final String VALUE = "点击查看";
    private final String HAED = "#";
    private final String LAST = "!A1";

    @Override
    public void beforeCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Row row, Head head, Integer integer, Integer integer1, Boolean aBoolean) {

    }

    @Override
    public void afterCellCreate(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, Cell cell, Head head, Integer integer, Boolean aBoolean) {

    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> list, Cell cell, Head head, Integer integer, Boolean aBoolean) {
        log.info("第{}行，第{}列写入完成。", cell.getRowIndex(), cell.getColumnIndex());
        if (cell.getRowIndex() > 0 && cell.getColumnIndex() == 9) {
            if (!TIPS.equals(cell.getStringCellValue())) {
//                CreationHelper createHelper = writeSheetHolder.getSheet().getWorkbook().getCreationHelper();
//                Hyperlink hyperlink = createHelper.createHyperlink(HyperlinkType.URL);
//                hyperlink.setAddress(HAED.concat("cell.getStringCellValue()").concat(LAST));
//                cell.setHyperlink(hyperlink);
                cell.setCellFormula("HYPERLINK(\"" + HAED.concat(cell.getStringCellValue()).concat(LAST) + "\",\"" + VALUE + "\")");
            }
        }
    }
}
